package com.oms.app.user.service.impl;

import java.util.List;
import java.util.Map;

import com.oms.app.sequence.service.impl.SequenceManager;
import com.oms.app.user.dao.IUserDAO;
import com.oms.app.user.model.User;
import com.oms.app.user.service.IUserService;
import com.oms.core.service.impl.GeneralService;
import com.oms.util.DataDeal;
import com.oms.util.DateUtil;

/**
 * @author Loong_Mit
 * @date Sep 1, 2012 7:03:38 PM
 */

public class UserServiceImpl extends GeneralService<User, String> implements IUserService {

	@Override
	public User get(String primaryKey) {
		return null;
	}

	@Override
	public void insert(User entity) {
	}

	@Override
	public int update(User entity) {
		return 0;
	}
	@Override
	public User getUserByPK(int PK) {
		return ((IUserDAO) dao).getUserByPK(PK);
	}
	
	@Override
	public User getUserByRegName(String regname) {
		return ((IUserDAO) dao).getUserByRegName(regname);
	}

	@Override
	public User login(User userForm) {
		User user = ((IUserDAO) dao).getUserByRegName(userForm.getRegname());
		if (validationUser(user, userForm)) {
			return user;
		}
		return null;
	}
	
	// 验证是否有该用户，并且密码一致 可能以后添加邮箱或者其他验证
	private boolean validationUser(User user, User userForm) {
		String password = userForm.getPassword();
		return (user != null && user.getPassword().equals(password));
	}

	@Override
	public User register(User regForm) {
		regForm = getBaseUserInfo(regForm);
		return ((IUserDAO) dao).register(regForm);
	}
	
	@Override
	public void addUser(User regForm) {
		regForm = getBaseUserInfo(regForm);
		((IUserDAO) dao).addUser(regForm);
	}
	
	@Override
	public User profile(User userForm) {
		if (userForm.getAdmin().length() == 0) {
			userForm.setAdmin("0");
		}
		if (userForm.getPassword().length() == 0) {
			User user = ((IUserDAO) dao).getUserByRegName(userForm.getRegname());
			userForm.setPassword(user.getPassword());
			user = null;
		}
		return ((IUserDAO) dao).profile(userForm);
	}
	
	@Override
	public void updateUser(User userForm) {
		((IUserDAO) dao).updateUser(userForm);
	}
	
	@Override
	public int deleteUser(int PK) {
		return ((IUserDAO) dao).deleteUesr(PK);
	}
	
	@Override
	public List<User> getUserList(Map<String, Object> param) {
		return ((IUserDAO) dao).getUserList(param);
	}
	
	private User getBaseUserInfo(User user) {
		if (user != null) {
			//生成主键ID
			user.setId(new Integer(SequenceManager.nextID("tuser")));
			user.setCreatetime(DataDeal.ObjCDate(DateUtil.getCurrentFullString(), "yyyy-MM-dd HH:mm:ss"));
			user.setStatus(Integer.valueOf(1));
			if (user.getPassword().length() == 0) { //未填写密码 默认密码为用户名
				user.setPassword(user.getRegname());
			}
			return user;
		} 
		return null;
	}

	@Override
	public boolean userAddRole(Map map) {
		map.put("id", new Integer(SequenceManager.nextID("user_role")));
		return ((IUserDAO) dao).userAddRole(map);
	}

	@Override
	public boolean isUserRole(Map map) {
		return ((IUserDAO) dao).isUserRole(map);
	}

	@Override
	public List getUserRole(User user) {
		return ((IUserDAO) dao).getUserRole(user);
	}
	@Override
	public List getUserModule(User user) {
		return ((IUserDAO) dao).getUserModule(user);
	}

	@Override
	public int delete(String primaryKey) {
		return 0;
	}

	@Override
	public List getAllModule() {
		return ((IUserDAO) dao).getAllModule();
	}

	@Override
	public boolean delUserRole(Map map) {
		return ((IUserDAO) dao).delUserRole(map);
	}

	@Override
	public int myShopId(int usid) {
		return ((IUserDAO) dao).myShopId(usid);
	}
	
}